<?php
/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author Oriotiori

 */

Class Default_Model_Comments extends Zend_Db_Table_Abstract{
		
	protected $_name = 'comments';


/**
 * getComments($To)
 * obtiene los comentarios
 * @param mixed $To: objeto con los datos del recurso.
 * @return array $data
 */
	function getComments($To=null)
        {

            $alldata=false;
            $this->_name= TABLE_PREFIX.'comments';
            $TO = new Default_Model_TO_Blog();
            try{

                $sql=$this->getAdapter()->select()->from(
                            array('c' => $this->_name),
                            array('c.Id', 'c.Comment')
                            )
                        ->joinLeft(
                            array('u' => TABLE_PREFIX.'users'),
                            'u.Id=c.User_Id',
                            array('User' =>'u.Username')
                        )
                        ->joinLeft(
                            array('i' => TABLE_PREFIX.'user_information'),
                            'i.User_Id=u.Id',
                            array('UserName' =>'i.Full_Name')
                        )
                        ->where('c.Topic_Id=?', $To['Id'])
                        ->where('c.Seccion=?', $To['Seccion'])
                        ->order('c.Id DESC');


                $data0 = $this->getAdapter()->fetchAll($sql);
                $rowCount = count($data0);

                if ($rowCount > 0){
                    foreach($data0 as $dt){
                        $TO->setId($dt['Id']);
                        $TO->setUserName($dt['UserName']);
                        $TO->setComment($dt['Comment']);
                        $TO->add();
                    }
                    $data = $TO->getAllData();
                    return $data;
                }

                return null;
            }catch(Exception $e)
            {
                $error=array($e->getFile(), 'Exception: '.$e->getMessage());
                $ExceptionHandler= new Default_Model_ExceptionHandler();
                $ExceptionHandler->saveErrors($error);
            }
	}


/**
 * addComments($To)
 * agrega nuevos comentarios
 * @param mixed $To: objeto con los datos del recurso.
 * @return array $data
 */
	function addComments($To=null)
        {
            $this->_name= TABLE_PREFIX.'comments';

            $datas= array(
                'User_Id' => $To['User_Id'],
                'Topic_Id' => $To['Id'],
                'Seccion' => $To['Seccion'],
                'Comment' => $To['Comentario'],
            );

            try{
                $sql=$this->getAdapter()->insert($this->_name, $datas);
                return true;
            }catch(Exception $e)
            {
                $error=array($e->getFile(), 'Exception: '.$e->getMessage());
                $ExceptionHandler= new Default_Model_ExceptionHandler();
                $ExceptionHandler->saveErrors($error);
            }
	}

}


?>